package com.tgpms.interfaces.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.interfaces.Request.HbHttpClient;
import com.tgpms.interfaces.common.CommonRequestDispose;
import com.tgpms.interfaces.hongbo.HbUrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created By Riven on 2020-9-14
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
@Slf4j
public class HongBo implements HongBoInterface {

    @Override
    public Result login(String username, String password) throws UnsupportedEncodingException {
        Result result = new Result();
        Map<String, Object> params = new HashMap<>(16);
        params.put("appsecret", username);
        params.put("appkey", password);
        String data = new HbHttpClient().login(HbUrl.LOGIN, params, HttpMethod.POST);
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(data);
        if (!parseObject.get("success").equals(true)) {
            result.setSuccess(false);
            result.setMsg(parseObject.get("message").toString());
            result.setData(parseObject);
        }
        result.setData(parseObject);
        return result;
    }

    @Override
    public Result getWorkArea(int pageindex, int pagesize) throws UnsupportedEncodingException {
        CommonRequestDispose dispose = new CommonRequestDispose();
        Map<String, Object> params = new HashMap<>(16);
        if (pageindex > 0)
            params.put("pageindex", pageindex);
        if (pagesize > 0)
            params.put("pagesize", pagesize);
        String data = new HbHttpClient().post(HbUrl.WORK_AREA, params, HttpMethod.GET);
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(data);
        // 判断是否登陆过期
        if (parseObject.get("state").equals(207)) {
            login("25152d15b347e65a9fc800697ec28cd8", "762919ff075b0ef29c30d320b12733ce");
            return getWorkArea(pageindex, pagesize);
        }
        return dispose.commonDispose(data);
    }

    @Override
    public Result getWorkProject(int pageindex, int pagesize) throws UnsupportedEncodingException {
        CommonRequestDispose dispose = new CommonRequestDispose();
        Map<String, Object> params = new HashMap<>(16);
        if (pageindex > 0)
            params.put("pageindex", pageindex);
        if (pagesize > 0)
            params.put("pagesize", pagesize);
        String data = new HbHttpClient().post(HbUrl.WORK_PROJECT, params, HttpMethod.GET);
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(data);
        // 判断是否登陆过期
        if (parseObject.get("state").equals(207)) {
            login("25152d15b347e65a9fc800697ec28cd8", "762919ff075b0ef29c30d320b12733ce");
            return getWorkProject(pageindex, pagesize);
        }
        return dispose.commonDispose(data);
    }

    @Override
    public Result getTube(int pageindex, int pagesize, Object parameters) throws UnsupportedEncodingException {
        CommonRequestDispose dispose = new CommonRequestDispose();
        Map<String, Object> params = new HashMap<>(16);
        if (pageindex > 0)
            params.put("pageindex", pageindex);
        if (pagesize > 0)
            params.put("pagesize", pagesize);
        if (parameters != null)
            params.put("parameters", parameters);
        String data = new HbHttpClient().post(HbUrl.TUBE, params, HttpMethod.GET);
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(data);
        // 判断是否登陆过期
        if (parseObject.get("state").equals(207)) {
            login("25152d15b347e65a9fc800697ec28cd8", "762919ff075b0ef29c30d320b12733ce");
            return getTube(pageindex, pagesize, parameters);
        }
        return dispose.commonDispose(data);
    }

    @Override
    public Result getTubeComplete(int pageindex, int pagesize, Object parameters) throws UnsupportedEncodingException {
        CommonRequestDispose dispose = new CommonRequestDispose();
        Map<String, Object> params = new HashMap<>(16);
        if (pageindex > 0)
            params.put("pageindex", pageindex);
        if (pagesize > 0)
            params.put("pagesize", pagesize);
        if (parameters != null)
            params.put("parameters", parameters);
        String data = new HbHttpClient().post(HbUrl.TUBE_COMPLETE, params, HttpMethod.GET);
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(data);
        // 判断是否登陆过期
        if (parseObject.get("state").equals(207)) {
            login("25152d15b347e65a9fc800697ec28cd8", "762919ff075b0ef29c30d320b12733ce");
            return getTubeComplete(pageindex, pagesize, parameters);
        }
        return dispose.commonDispose(data);
    }

    @Override
    public Result getMahole(int pageindex, int pagesize, Object parameters) throws UnsupportedEncodingException {
        CommonRequestDispose dispose = new CommonRequestDispose();
        Map<String, Object> params = new HashMap<>(16);
        if (pageindex > 0)
            params.put("pageindex", pageindex);
        if (pagesize > 0)
            params.put("pagesize", pagesize);
        if (parameters != null)
            params.put("parameters", parameters);
        String data = new HbHttpClient().post(HbUrl.MAHOLE, params, HttpMethod.GET);
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(data);
        // 判断是否登陆过期
        if (parseObject.get("state").equals(207)) {
            login("25152d15b347e65a9fc800697ec28cd8", "762919ff075b0ef29c30d320b12733ce");
            return getMahole(pageindex, pagesize, parameters);
        }
        return dispose.commonDispose(data);
    }

    @Override
    public Result getMaholeComplete(int pageindex, int pagesize, Object parameters) throws UnsupportedEncodingException {
        CommonRequestDispose dispose = new CommonRequestDispose();
        Map<String, Object> params = new HashMap<>(16);
        if (pageindex > 0)
            params.put("pageindex", pageindex);
        if (pagesize > 0)
            params.put("pagesize", pagesize);
        if (parameters != null)
            params.put("parameters", parameters);
        String data = new HbHttpClient().post(HbUrl.MAHOLE_COMPLETE, params, HttpMethod.GET);
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(data);
        // 判断是否登陆过期
        if (parseObject.get("state").equals(207)) {
            login("25152d15b347e65a9fc800697ec28cd8", "762919ff075b0ef29c30d320b12733ce");
            return getMaholeComplete(pageindex, pagesize, parameters);
        }
        return dispose.commonDispose(data);
    }

    @Override
    public Result getTubeDocument(int pageindex, int pagesize) throws UnsupportedEncodingException {
        CommonRequestDispose dispose = new CommonRequestDispose();
        Map<String, Object> params = new HashMap<>(16);
        if (pageindex > 0)
            params.put("pageindex", pageindex);
        if (pagesize > 0)
            params.put("pagesize", pagesize);
        String data = new HbHttpClient().post(HbUrl.TUBE_DOCUMENT, params, HttpMethod.GET);
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(data);
        // 判断是否登陆过期
        if (parseObject.get("state").equals(207)) {
            login("25152d15b347e65a9fc800697ec28cd8", "762919ff075b0ef29c30d320b12733ce");
            return getTubeDocument(pageindex, pagesize);
        }
        return dispose.commonDispose(data);
    }
}
